package com.saifan.wyy_ov.ui.onlishop;
import java.util.ArrayList;
import java.util.List;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.common.BaseActivity;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.OrderBean;
import com.saifan.wyy_ov.data.bean.Orderdetail;
import com.saifan.wyy_ov.data.bean.SubmitOrderBean;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;

import com.saifan.wyy_ov.ui.view.MaterialDialog;
import com.saifan.wyy_ov.utils.LogUtil;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;

/***
 * 提交订单
 * // 购买类型1 团购 2促销,默认为0
 * 	private double price;
	private int storeId;
	private String goodsName;
	private int goodsId;
	private String storeName;
	private String photo;
 * @author Administrator
 *
 */

public class SubmitOrderActivity extends BaseActivity implements OnClickListener, TextWatcher {
	OrderBean orderBean;
	GetData getData;
	Gson gson;
	// 购买类型1 团购 2促销
	private int type;
	private String note = "";
	int count = 1;
	private double price;
	private int storeId;
	private String goodsName;
	private int goodsId;
	private String storeName;
	private int maxCount;
	private String photo;

	public com.saifan.wyy_ov.ui.view.NoScrollListView listView;
	public TextView tv_allTotal;
	public TextView tv_tel;
	public TextView tv_store_assess;
	public TextView tv_address;
	private ArrayList<OrderBean> orderBeans;
	public TextView tv_goods_name;
	public TextView tv_price;
	public TextView tv_total, tv_count;
	public EditText ed_remark;
	public Button bt_add, bt_sub;
	private MaterialDialog addressDialog, telDialog;
	private EditText editTel, editaddress;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_submit_order);
		//Bundle bundle = getIntent().getExtras();

		type =getIntent().getIntExtra("buy_type", -1);
		price = getIntent().getDoubleExtra("price", 0);
		storeId = getIntent().getIntExtra("storeId", 1);
		goodsName = getIntent().getStringExtra("goodsName");
		goodsId = getIntent().getIntExtra("goodsId", -1);
		storeName = getIntent().getStringExtra("storeName");
		maxCount = getIntent().getIntExtra("maxCount", -1);
		photo = getIntent().getStringExtra("photo");
		bindViews();

		gson = new Gson();
		getData = new GetData();
	}


	private void bindViews() {
		addressDialog = new MaterialDialog(this);
		telDialog = new MaterialDialog(this);
		 setEnabledNavigation(true);
		listView = (com.saifan.wyy_ov.ui.view.NoScrollListView) findViewById(R.id.list);
		tv_allTotal = (TextView) findViewById(R.id.tv_allTotal);
		tv_tel = (TextView) findViewById(R.id.tv_tel);
		tv_store_assess = (TextView) findViewById(R.id.tv_store_assess);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_goods_name = (TextView) findViewById(R.id.tv_goods_name);
		tv_price = (TextView) findViewById(R.id.tv_price);
		tv_total = (TextView) findViewById(R.id.tv_total);
		ed_remark = (EditText) findViewById(R.id.ed_remark);
		bt_add = (Button) findViewById(R.id.bt_add);
		bt_sub = (Button) findViewById(R.id.bt_sub);
		tv_count = (TextView) findViewById(R.id.tv_count);
		bt_add.setOnClickListener(this);
		bt_sub.setOnClickListener(this);
		tv_goods_name.setText(goodsName);
		tv_count.setText(count + "");
		tv_price.setText(PriceUtils.getPrice(price) + "");
		tv_total.setText(PriceUtils.getPrice(count * price) + "");
		ed_remark.addTextChangedListener(this);
		tv_address.setText(StringUtil.returnShow(getRoomInfoBean().getXMDA_XMMC() + getRoomInfoBean().getWYFJDA_FJMC()));
		tv_tel.setText(StringUtil.returnShow(getRoomInfoBean().getWYKHDA_SJ()));
		tv_allTotal.setText(PriceUtils.getPrice(count * price)+"元");
	}
	public void submitOrder( final View view) {
		// TODO Auto-generated method stub


		String tel=tv_tel.getText().toString().trim();
		String address=tv_address.getText().toString().trim();

		if (tel.isEmpty() ){
			changeTel(tv_tel);

		}
		 else if (address.isEmpty()){
			changeAddress(tv_address);

		}else {
			view.setEnabled(false);
			buildOrders();


			getData.getDataJson(this, Api.GroupBuy.SUBMITORDER, orderBeans, "正在提交,请稍候!", new ResponseData() {
				@Override
				public void onSuccess(String json) {
					// TODO Auto-generated method stub
					// ToastUtil.showToast(SubmitOrderActivity.this, json);
					List<SubmitOrderBean> list = gson.fromJson(json, new TypeToken<List<SubmitOrderBean>>() {
					}.getType());
					if (list.size() > 0) {
						Intent intentGoPayActivity = new Intent(SubmitOrderActivity.this, PayActivity.class);
						double total = 0;
						for (SubmitOrderBean submitOrderBean : list) {
							total += Double.valueOf(submitOrderBean.getAmount());
						}
						int paymentWay = 2;
						intentGoPayActivity.putExtra("paymentWay", paymentWay);
						intentGoPayActivity.putExtra("total", total);
						intentGoPayActivity.putExtra("SubmitOrderBeans", json);
						view.setEnabled(true);
						finish();
						startActivity(intentGoPayActivity);
					} else {
						LogUtil.i(json);
					}
				}
				@Override
				public void onFailed(String str) {
					// TODO Auto-generated method stub
					LogUtil.e(str);
				}
			});
		}


	}
	public void buildOrders() {
		orderBeans = new ArrayList<OrderBean>();
		OrderBean orderBean = new OrderBean();
		orderBean.setAddress(tv_address.getText().toString());
		orderBean.setContact(getRoomInfoBean().getWYKHDA_KHMC());
		orderBean.setOrderPeopleID(getRoomInfoBean().getWYKHDA_ZJ());
		orderBean.setUserID(storeId + "");
		orderBean.setTel(tv_tel.getText().toString());
		ArrayList<Orderdetail> orderdetails = new ArrayList<Orderdetail>();
		Orderdetail orderdetail = new Orderdetail();
		orderdetail.setCount(count);
		orderdetail.setPrice(price);
		orderdetail.setUserID(storeId + "");
		orderdetail.setType(type);
		orderdetail.setGoodsID(goodsId + "");
		orderdetail.setGoodsName(goodsName);
		orderdetail.setCoverPhoto(photo);
		orderdetail.setNote(note);
		orderdetails.add(orderdetail);
		orderBean.setOrderdetail(orderdetails);
		orderBeans.add(orderBean);
	}
	public void changeAddress(View view) {
		// TODO Auto-generated method stub
		if (addressDialog != null) {
			View editView = getLayoutInflater().from(this).inflate(R.layout.address_edit, null);
			editaddress = (EditText) editView.findViewById(R.id.edit_address);
			addressDialog.setView(editView).setPositiveButton("是", new OnClickListener() {
				@Override
				public void onClick(View v) {
					tv_address.setText(editaddress.getText().toString());
					addressDialog.dismiss();
				}
			}).setNegativeButton("否", new OnClickListener() {
				@Override
				public void onClick(View v) {
					addressDialog.dismiss();
				}
			}).setCanceledOnTouchOutside(false)
			.setOnDismissListener(new DialogInterface.OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
				}
			}).show();
		}
	}
	public void changeTel(View view) {
		// TODO Auto-generated method stub
		if (telDialog != null) {
			View editView = getLayoutInflater().from(this).inflate(R.layout.tel_edit, null);
			editTel = (EditText) editView.findViewById(R.id.edit_tel);
			telDialog.setView(editView).setPositiveButton("是", new OnClickListener() {
				@Override
				public void onClick(View v) {
					tv_tel.setText(editTel.getText().toString());
					telDialog.dismiss();
				}
			}).setNegativeButton("否", new OnClickListener() {
				@Override
				public void onClick(View v) {
					telDialog.dismiss();
				}
			}).setCanceledOnTouchOutside(false)
			.setOnDismissListener(new DialogInterface.OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
				}
			}).show();
		}
	}
//	public void add(View rootview) {
//		// TODO Auto-generated method stub
//		if (type==1) {
//			if (count < maxCount) {
//				count++;
//			}
//		}else{
//			count++;
//		}
//		tv_count.setText(count + "");
//		tv_total.setText(PriceUtils.getPrice(price * count )+ "");
//		tv_allTotal.setText(PriceUtils.getPrice(count * price)+"元");
//	}
//	public void sub(View rootview) {
//		// TODO Auto-generated method stub
//		if (count > 0) {
//			count--;
//			tv_count.setText(count + "");
//			tv_total.setText(PriceUtils.getPrice(price * count )+ "");
//			tv_allTotal.setText(PriceUtils.getPrice(count * price)+"元");
//		}
//	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == android.R.id.home) {
			this.finish();
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if (arg0.getId() == R.id.bt_add) {
			if (type==1) {
				if (count < maxCount) {
					count++;
				}
			}else{
				count++;
			}
			tv_count.setText(count + "");
			tv_total.setText(PriceUtils.getPrice(price * count )+ "");
			tv_allTotal.setText(PriceUtils.getPrice(count * price)+"元");
		} else if (arg0.getId() == R.id.bt_sub) {
			if (count > 1) {
				count--;
				tv_count.setText(count + "");
				tv_total.setText(PriceUtils.getPrice(price * count )+ "");
				tv_allTotal.setText(PriceUtils.getPrice(count * price)+"元");
			}
		}
	}
	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		note = arg0.toString();
	}
}
